//
//  CircleListViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/12/1.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "CircleListViewModel.h"
#import "CircileListTableViewCell.h"
#import "CircleListImageViewTVCell.h"
#import "CircleListrHeaderFooterView.h"

@interface CircleListViewModel()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *selfTableView;
    UIView *selfView;
}

@end

@implementation CircleListViewModel

- (instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        selfView = superView;
        CGFloat navHeight;
        if (iPhoneX) {
            navHeight = 133;
        } else {
            navHeight = 113;
        }
        tableView.height = KScreenHeight - navHeight;
        selfTableView = tableView;
        selfTableView.delegate = self;
        selfTableView.dataSource = self;
        selfTableView.estimatedRowHeight = 0;
        selfTableView.rowHeight = UITableViewAutomaticDimension;
        selfTableView.mj_header.automaticallyChangeAlpha = YES;
        selfTableView.showsVerticalScrollIndicator = NO;
        [selfTableView registerClass:[CircleListrHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"CircleListrHeaderFooterView"];

        [superView addSubview:selfTableView];
        [self addReleaseBtn];
    }
    return self;
}

- (void)addReleaseBtn
{
    GLGWButton *releaseBtn = [[GLGWButton alloc] initInSuperView:selfView];
//    [releaseBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    [releaseBtn setBackgroundColor:kRandomColor];
    CGFloat btnh = 0;
    if (iPhoneX) {
        btnh = -kWidthProportion_In_iPhoneTriploid(39);
    }
    [releaseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self->selfView).offset(-kWidthProportion_In_iPhoneTriploid(15));
        make.width.height.equalTo(@(kWidthProportion_In_iPhoneTriploid(50)));
        make.bottom.equalTo(self->selfTableView.mas_bottom).offset(btnh);
    }];
    [releaseBtn addTarget:self action:@selector(releaseBtnAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)releaseBtnAction:(GLGWButton *)sender
{
    if (self.Block_Release) {
        self.Block_Release(sender);
    }
}

- (void)refreshHeader
{
    
}

- (void)loadMoreCircle
{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return kHeightProportion_In_iPhoneTriploid(153);
    } else {
        return kHeightProportion_In_iPhoneTriploid(275);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        CircileListTableViewCell *cell = [CircileListTableViewCell cellWithTableView:tableView];
        cell.Block_LikeClickAction = ^(GLGWLabel * _Nullable click_lab) {
            // 点击喜欢
            DLog(@"喜欢");
        };
        return cell;
    } else {
        CircleListImageViewTVCell *cell = [CircleListImageViewTVCell cellWithTableView:tableView];
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CircleListrHeaderFooterView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"CircleListrHeaderFooterView"];
    kWeakSelf(self);
    headerView.Block_Btn = ^(GLGWButton * _Nullable btn) {
        if (weakself.Block_more) {
            weakself.Block_more(btn);
        }
    };
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return kHeightProportion_In_iPhoneTriploid(228);
}

@end
